package net.atomtree.util;

import net.atomtree.handler.APPHandler;
import net.atomtree.handler.JobHandler;
import net.atomtree.handler.RestHandler;
import net.atomtree.handler.StageHandler;
import net.sf.json.JSONObject;

public class IndexService {
		//	StringBuffer url=new StringBuffer();
		//	url.append("http://101.204.243.107:4040/api/v1");
		public JSONObject countSpeed (StringBuffer url) throws Exception{
			String URL=url.toString();
			StringBuffer appUrl=new StringBuffer(url.toString());
			StringBuffer jobUrl=new StringBuffer(url.toString());
			StringBuffer stageUrl=new StringBuffer(url.toString());
			System.out.println("appurl:"+appUrl);
			System.out.println("jobUrl:"+jobUrl);
			System.out.println("stageUrl:"+stageUrl);
			
			System.out.println("进入SpeedService。countSpeed");
			System.out.println("SpeedService。countSpeed开始第一阶段:得到app的json对象和appName");
			System.out.println("调用入参url，得到app的rest jsonArr");
			String JsonArr= RestHandler.getApplication(appUrl);
	
			
			APPHandler appHandler=new APPHandler();
			System.out.println("解析app的rest jsonArr");
			String appName=appHandler.getAppNameByString(JsonArr);
			if(null==appName||appName.equals(null)){
				System.out.println("appName解析失败name为null");
				throw new Exception("appName 为null" );
			}
			appName=appName.replace(" ", "%20");
			System.out.println("解析完成 appName:"+appName);
			
			/*----------------------------------------------------------------------------------------------------*/
			System.out.println("SpeedService。countSpeed开始第二阶段,将appName传入url得到job json对象");
			System.out.println("传入url和app那么得到jobArr");
			String jobArr=RestHandler.getApplicationJob(jobUrl, appName);
			if(null==jobArr||jobArr.equals(" "))
				throw new Exception("jobArr为null");
			JobHandler job=new JobHandler();
			JSONObject jobJson=job.getJob(jobArr);
			System.out.println(jobJson.get("stageIds"));
			System.out.println(jobJson.get("stageIds").toString());
			String stages=jobJson.get("stageIds").toString();
			String stageArrLength=stages.substring(stages.lastIndexOf("[")+1, stages.lastIndexOf("]"));
			if(stageArrLength.length()>2){
				stageArrLength=stageArrLength.substring(stageArrLength.indexOf(","));
			}
			System.out.println("*************************************debug********************************************");
			System.out.println("stages="+stages);
			System.out.println("stages总共有"+stageArrLength+"个");
			System.out.println("*************************************debug********************************************");
			/*----------------------------------------------------------------------------------------------------*/
			System.out.println("SpeedService。countSpeed开始第三阶段,将appName传入url得到stage json对象");
			System.out.println("传入appName，得到stageArr");
			String stageArr= RestHandler.getApplicationStages(stageUrl, appName);
			StageHandler stageHandler=new StageHandler();
			System.out.println("开始解析stageArr");
			JSONObject stageJson=stageHandler.getStage(stageArr);
			if(null!=stageJson){
				System.out.println("解析成功返回stageJson对象");
//				return stageJson;
			System.out.println("************************************change********************************************");
			//根据stages数组的长度建立存放时间的数组
			int stageArrlength=Integer.parseInt(stageArrLength);
			System.out.println("定义长度为"+stageArrlength+"的数组");
			if(stageArrlength==0){
				//迭代运算只有一个stage，需要统计task任务的数量来计算执行数据的处理条数
				//stages rest的numCompleteTasks
				stageJson.element("shuffleReadRecords", stageJson.get("numCompleteTasks"));
			}else{
				//有多个stage，则需要将多个stage的时间进行统计
				int executeRunTimeSum[]=new int[stageArrlength+1];
				//String stageArr= RestHandler.getApplicationStages(stageUrl, appName); 这里要调用有Id的stage rest
				for(int i=0;i<=stageArrlength;i++){
					String stageArrById=RestHandler.getApplicationStagesByAppappnameAndStageappname(new StringBuffer(URL),appName, new Integer(i).toString());
					System.out.println("stageArrById:"+stageArrById);
					//得到的从小到大的stageId的stageArr,循环的转换成json对象，读取json对象的executeRunTime
					JSONObject stageObjectById=stageHandler.getStage(stageArrById);
					System.out.println("stageObjectById.get(stageId):"+stageObjectById.get("stageId"));
					int stageId=Integer.parseInt( stageObjectById.get("stageId").toString());
					int rumTime=Integer.parseInt( stageObjectById.get("executorRunTime").toString());
					System.out.println("当前stageId为"+stageId+"的rumTime:"+rumTime);
					//根据stageId当做数组下标，存放executeRunTime
					executeRunTimeSum[stageId]=rumTime; 
				}
				int runTimeSum=0;
				for(int i=0;i<executeRunTimeSum.length;i++){
					runTimeSum+=executeRunTimeSum[i];
				}
				System.out.println("统计求和后runTimeSum的值为"+runTimeSum);
				//将executeRunTime的值放进 stage Rest不带id的json对象中
				stageJson.element("executorRunTime", runTimeSum);
			}
			System.out.println("************************************change********************************************");
			return stageJson;
			}
			System.out.println("stageJson对象为null解析失败返回null");
			return null;
		}
		
		public JSONObject countRumTime(StringBuffer url,String appname) throws Exception{
			JobHandler job=new JobHandler();
			String jsonArr=RestHandler.getApplicationJob(url, appname);
			JSONObject jobJson=job.getJob(jsonArr);
			System.out.println("stageIds"+jobJson.get("stageIds"));
			return null;
		}
}
